﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VSDBM.Code.dao;
using VSDBM.Code.dto;
using VSDBM.Code.functions;
using System.Threading;

using System.Text;

public partial class Visual_Procedures_Blast_BlastNTool : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillOrganism();
        FillGroups();
    }
    protected void FillOrganism()
    {
        DataSet ds = OrganismDAO.select("WHERE status=1", "ORDER BY name");
        //verifica erro na querry
        if (ds.Tables.Count == 0)
        {
            Exception ex = new Exception("Table not found!");
            throw ex;
        }
        //verifica se existem organismos cadastrados.
        else if (ds.Tables[0].Rows.Count == 0)
        {
            DataRow dr = ds.Tables[0].NewRow();
            ds.Tables[0].Rows.InsertAt(dr, 0);
            ds.Tables[0].Rows[0]["name"] = "NO ORGANISMS FOUND";
            ds.Tables[0].Rows[0]["id"] = "0";

        }
        //Insere os organismos no Dropdown List(ddOrganism).

        ddOrganism.DataSource = ds.Tables[0];
        ddOrganism.DataTextField = "name";
        ddOrganism.DataValueField = "id";
        ddOrganism.DataBind();

    }
    protected void FillGroups()
    {
        DataSet ds = GroupsDAO.select("", "ORDER BY description");
        //verifica erro na querry
        if (ds.Tables.Count == 0)
        {
            Exception ex = new Exception("Table not found!");
            throw ex;
        }
        //verifica se existem organismos cadastrados.
        else if (ds.Tables[0].Rows.Count == 0)
        {

            DataRow dr = ds.Tables[0].NewRow();
            ds.Tables[0].Rows.InsertAt(dr, 0);
            ds.Tables[0].Rows[0]["description"] = "NO GROUPS FOUND";
            ds.Tables[0].Rows[0]["id"] = "0";

        }
        //Insere os organismos no Dropdown List(ddOrganism).

        ddgroup.DataSource = ds.Tables[0];
        ddgroup.DataTextField = "description";
        ddgroup.DataValueField = "id";
        ddgroup.DataBind();
    }
    protected void btnRunBlast_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtQuerrySequence.Text != "")
            {
                String QuerrySequence = txtQuerrySequence.Text;
                QuerrySequence = QuerrySequence.ToUpper();
                QuerrySequence = QuerrySequence.Replace("\r\n", "");
                QuerrySequence = QuerrySequence.Replace(" ", "");
                txtQuerrySequence.Text = QuerrySequence;
                String[] parametros = new String[5];
                parametros[0] = QuerrySequence;
                parametros[1] = Convert.ToString(RadioList.SelectedIndex);
                if (RadioList.SelectedIndex == 0)
                {
                    parametros[2] = ddOrganism.SelectedValue;
                }
                else if (RadioList.SelectedIndex == 1)
                {
                    parametros[2] = ddgroup.SelectedValue;
                }
                if (parametros[2] == "0")
                {
                    Exception exp = new Exception("NO DATABASE SELECTED!");
                    HttpContext.Current.Session["exception"] = exp.Message;
                    Response.Redirect("../../../Visual/Errors/error.aspx");
                }
                VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
                parametros[3] = Convert.ToString(usuariologado.id);
                if (txtMaxResults.Text == "" || txtMaxResults.Text == null)
                {
                    parametros[4] = "5";
                }
                else
                {
                    parametros[4] = txtMaxResults.Text;
                }
                runBlast(parametros);
            }
            else
            {
                Exception ex = new Exception("Invalid Sequence");
                HttpContext.Current.Session["exception"] = ex.Message;
                Response.Redirect("../../../Visual/Errors/error.aspx");
            }
        }
        catch (Exception exs)
        {

            HttpContext.Current.Session["exception"] = exs.Message;
            Response.Redirect("../../../Visual/Errors/error.aspx");
        }


    }
    protected void runBlast(object param)
    {
       
    }

    protected void RadioList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioList.SelectedIndex == 0)
        {
            ddgroup.Visible = false;
            ddOrganism.Visible = true;

        }
        else if (RadioList.SelectedIndex == 1)
        {
            ddgroup.Visible = true;
            ddOrganism.Visible = false;
        }

    }

    protected void btnGetIt_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtGBID.Text == "")
            {
                Exception ex = new Exception("No GB id found");
                HttpContext.Current.Session["exception"] = ex.Message;
                Response.Redirect("../../../Visual/Errors/error.aspx");
            }
            VSDBM.Code.dto.Sequence seq = zeseeker.runSingleeFetchPOST(txtGBID.Text, "nuccore");
            txtQuerrySequence.Text = seq.sequence;

        }
        catch (Exception exp)
        {

            HttpContext.Current.Session["exception"] = exp.Message;
            Response.Redirect("../../../Visual/Errors/error.aspx");
        }
    }
}